package com.cskaoyan.introduction.edition2;

import java.util.Scanner;

/**
 * @author zhangshuai@Cskaoyan.onaliyun.com.
 * @version 1.0
 */
public class Demo1 {


    static boolean flag = true;
    public static void main(String[] args) {

        // 先不停的输出问候语句
        sayHelloRecycling();

//    System.out.println("after sayHelloRecycling");
        //接收键盘输入，根据键盘输入，决定是否输出问候语句
        waitToStop();

    }

    // 接收键盘输入
    public static void waitToStop() {
        // Thread类或Thread子类对象，代表一个线程
        WaitToStop waitToStop = new WaitToStop();
        // 启动一个线程
        waitToStop.start();

    }


    // 不停的向屏幕输出问候语句
    public static void sayHelloRecycling() {
        // Thread类或Thread子类对象，代表一个线程
        SayHello sayHello = new SayHello();
        // 启动一个线程
        sayHello.start();
    }

}

class SayHello extends Thread {
    public void run() {
        while (Demo1.flag) {
            System.out.println("哈哈，你好");

            try {
                // 暂停3秒
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class WaitToStop extends Thread {
    @Override
    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String s = scanner.nextLine();
            if ("gun".equals(s)) {
                Demo1.flag = false;
                break;
            }
        }
    }
}

class MyThread extends Thread {
    public MyThread(Runnable run) {
        super(run);
    }

    @Override
    public void run() {

        System.out.println("Thread子类的run方法");

    }
}

